Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

263
Views
"error: duplicate case value" error when using goto in switch statement

I was trying to use the goto statement to travel between different switch-cases. I understand, it's not preferable to use goto as it would make the program tough to understand, but I really need it.

Here's the example version of my code:

switch(something){
    case "c1":
        //some code
        break;
    case "c2":
       //some code
       break;
    case "c3":
        if(condition1)
            goto case "c1";
        if(condition2)
            goto case "c2";
    default:
        break;
}

Now, when I run the code, I get the following error error: duplicate case value which applies to both goto case "c1" and goto case "c2".

I'm not sure why the compiler is thinking the goto on a switch-case is redefining another case in the switch statement with the same condition and hence throwing that duplicate error. Any help and reasons for this error is appreciated.

Thanks!

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

In any case the switch statement does not make a sense because you are using string literals as case labels.

switch(something){
    case "c1":
        //some code
        break;
    case "c2":
       //some code
       break;
    case "c3":
        if(condition1)
            goto case "c1";
        if(condition2)
            goto case "c2";
    default:
        break;
}

The expression in the switch statement is converted to an integral or enumeration type that can not be implicitly converted to pointers or string literals.

Maybe you need to use character literals as for example

case 'c1':

The second problem is you may not use case labels with goto statements. You may use with the goto statements only labels that are presented as identifiers. The syntax of the goto statement is

goto identifier ;

So the compiler thinks that the case labels used in goto statements redefine already introduced case labels.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!